import axios from "axios";
import { defineStore } from "pinia";
import { shallowRef } from "vue";
import LayoutVue from "@/components/layout/Layout.vue";
const routMap = import.meta.glob(`../view/**/*.vue`)
// console.log(routMap)
export const usePermissionStore = defineStore('PermissionStore',()=>{
    const routerList = shallowRef([])

    const genRoutor = (routerList:any[])=>{
        routerList.forEach(item=>{
            const component = item.component
            if (component === 'Layout') {  
                // 如果组件是LayoutVue
                item.component = LayoutVue
            }else{
                // item.component = () =>import(`@/view${component}`)
                item.component = routMap[`../view${component}`]
          
            }
            if (item.children && item.children.length > 0) {
                genRoutor(item.children)
            }

        })
    }


    const initRouterList = async ()=>{
        const res = await axios.get('/menu')
        // console.log('====',res)
        genRoutor(res.data.data)
        // console.log(res)
        routerList.value = res.data.data
    }
    return {
        routerList,
        initRouterList
    }
})